﻿using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using eXamineControls.SystemDescription.Draw.Item;

namespace eXamineControls.SystemDescription.Draw
{
    public partial class DrawSystemPanel : UserControl
    {
        private bool _expectingClick;
        private DrawType _type;
        private List<ISystemDescriptionItem> _components;
        private bool _connectionMode;
        private ISystemDescriptionItem _first;
        private ISystemDescriptionItem _second;

        public DrawSystemPanel()
        {
            InitializeComponent();
            this._expectingClick = false;
            this._type = DrawType.Component;
            this.AutoScroll = true;
            this._components = new List<ISystemDescriptionItem>();
        }

        #region API
        public void StartDraw (DrawType type)
        {
            if (_connectionMode) this.EndConnectionMode();
            this._expectingClick = true;
            this._type = type;
            this.drawArea.Cursor = Cursors.UpArrow;
        }
        #endregion

        #region Auxiliar
        private void BeginConnectionMode ()
        {
            this._connectionMode = true;
            foreach (ISystemDescriptionItem item in this._components)
                item.BeginConnectionMode();
            this._first = null;
            this._second = null;
        }
        private void EndConnectionMode()
        {
            this._connectionMode = false;
            this._first = null;
            this._second = null;
            foreach (ISystemDescriptionItem item in this._components)
                item.EndConnectionMode();
        }
        private void DrawComponent (int x, int y)
        {
            ComponentItem comp = new ComponentItem();
            comp.Location = new Point(x - (comp.Size.Width / 2), y - (comp.Size.Height / 2));
            comp.ColorBackground = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            comp.ColorMouseInside = System.Drawing.Color.FromArgb(((int)(((byte)(214)))), ((int)(((byte)(214)))), ((int)(((byte)(214)))));
            comp.ColorSelected = System.Drawing.Color.Silver;
            comp.ColorDrag = System.Drawing.Color.FromArgb(((int)(((byte)(200)))), ((int)(((byte)(200)))), ((int)(((byte)(200)))));
            comp.Title = "Component" + (this._components.Count + 1);
            
            this._components.Add(comp);
            this.drawArea.Controls.Add(comp);
            this.drawArea.Controls.SetChildIndex(comp, 0);
            this._expectingClick = false;
            this.drawArea.Cursor = Cursors.Default;
        }
        private void DrawBarrier(int x, int y)
        {
            BarrierItem comp = new BarrierItem();
            comp.Location = new Point(x - (comp.Size.Width / 2), y - (comp.Size.Height / 2));
            comp.ColorBackground = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            comp.ColorMouseInside = System.Drawing.Color.FromArgb(((int)(((byte)(214)))), ((int)(((byte)(214)))), ((int)(((byte)(214)))));
            comp.ColorSelected = System.Drawing.Color.Silver;
            comp.ColorDrag = System.Drawing.Color.FromArgb(((int)(((byte)(200)))), ((int)(((byte)(200)))), ((int)(((byte)(200)))));
            comp.Title = "Barrier" + (this._components.Count + 1);
            comp.ItemSelected += new System.EventHandler(comp_ItemSelected);

            this._components.Add(comp);
            this.drawArea.Controls.Add(comp);
            this.drawArea.Controls.SetChildIndex(comp, 0);
            this._expectingClick = false;
            this.drawArea.Cursor = Cursors.Default;
        }
        private void DrawArrow ()
        {
            if (this._first == null)
            {

            }
            else
            {
                
            }
        }
        #endregion

        #region Event Handlers
        private void comp_ItemSelected(object sender, System.EventArgs e)
        {
            if (this._first == null) this._first = (ISystemDescriptionItem) sender;
            else if (this._second == null)
            {
                this._second = (ISystemDescriptionItem)sender;
                this.EndConnectionMode();
            }
        }
        private void DrawPanel_MouseClick(object sender, MouseEventArgs e)
        {

            if (this._expectingClick)
            {
                switch (_type)
                {
                    case DrawType.Component:
                        this.DrawComponent(e.X, e.Y);
                        break;
                    case DrawType.Barrier:
                        this.DrawBarrier(e.X, e.Y);
                        break;
                }
            }
        }
        #endregion

        #region API
        #endregion
    }
}
